import React from "react";
import {Checkbox, Modal, Tooltip} from "antd";
import BaseComponent from "../../../redare/components/BaseComponent";
import {_changeData} from "../../../common/Utils";
import Grid from "../../../redare/components/grid/Grid";
import DictionaryPresenter from "../../../.gen/presenter/DictionaryPresenter";

export default class DictionarySelectModal extends BaseComponent {

  static defaultProps = {
    title: '字典',
    onOk: ()=>{}
  };

  constructor(props) {
    super(props);
    this.state = {
      confirmLoading: false,
      modalShow: false,
    };
    this.dicPresenter = new DictionaryPresenter(this)
  }

  componentDidMount() {
    // this._findDictionaryList();
  }

  //查询字典列表
  _findDictionaryList(body={}) {

    if (this.grid) {
      this.grid.showLoadingIndicator();
    }
    this.dicPresenter.findDictionaryTree(
      body,
      {},
      (result) => {
        let {data} = result;
        if (data && this.grid) {
          let parentId = data[0].parentId;

          let changeData = _changeData(data,parentId);

          // this.grid.setData(changeData);
          this.grid.setData(data);
          this.grid.closeLoadingIndicator()
        }
      },
      () => {
        if (this.grid) {
          this.grid.closeLoadingIndicator();
        }
      })
  }

  openModal(param){
    this.setState({modalShow: true});
    this._findDictionaryList(param);
  }

  onOK(){
    let data = this.selectedRows;
    this.setState({modalShow: false});
    if( this.props.onOk && typeof this.props.onOk == "function"){
      this.props.onOk(data);
    }

  }

  render() {
    let state = this.state;
    return (
      <div>
        <Modal
          title={"选择-"+this.props.title}
          okText="保存"
          cancelText="取消"
          visible={state.modalShow}
          onOk={()=>{this.onOK()}}
          onCancel={() => {this.setState({modalShow: false})}}
          centered={true}
          maskClosable={false}
          destroyOnClose={true}
          // forceRender={true}
          width={'70%'}
          bodyStyle={{padding: '10px 24px 20px 24px'}}
        >
          <Grid
            ref={(o) => {
              this.grid = o
            }}
            rowSelection={{
              onChange: (selectedRowKeys, selectedRows) => {
                this.selectedRowKeys = selectedRowKeys;
                this.selectedRows = selectedRows;
              },
              getCheckboxProps: record => ({
                disabled: record.leaf == false,
                name: record.name
              }),
              renderCell:(checked, record, index, originNode)=>{
                if( record.leaf == false ){
                    return <Tooltip title={'只能选择叶节点'}><Checkbox disabled={true}></Checkbox></Tooltip>
                }
                return originNode;
              }
            }}
            _initLoading={true}
            _showPage={false}
            bordered={false}
            rowKey={(v) => v.id}
            scroll={{ y: '50vh'}}
            columns={[
              {
                title: '字典名称',
                dataIndex: 'dicName'
              },
              {
                title: '字典编码',
                dataIndex: 'dicCode'
              },
              {
                title: '描述',
                dataIndex: 'des'
              },
              {
                title: '状态',
                dataIndex: 'status',
                render: (text, record, index) => {
                  switch (text) {
                    case 1:
                      return (
                        <span style={{color: 'green'}}>● 启用</span>
                      );
                    case 0:
                      return (
                        <span style={{color: 'red'}}>● 禁用</span>
                      )
                  }
                }
              },
            ]}
          />
        </Modal>
      </div>
    )
  }


}